﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;

namespace ReportTracking
{
    public class Utilities
    {
        public static string EditString(string sInput, string sPattern, string sReplace, int nNumberWord, int nLength = 100, int nNumberCut =-1)
        {
            //string pattern = @"\s"; //Khoang trang
            //                  @"(\w+)\s+(car)"; tu + khoang trang + voi tu 'car' 
            if (sInput.Length > 0)
            {
                
                int[] arrPosition = GetPositionParttern(sInput, sPattern);
                int nNumberReplace = arrPosition.Length/nNumberWord;// So lan thay the
                if (nNumberReplace > 0)
                {
                    List<string> sSplit = new List<string>();
                    //Thay the o nhung vi tri nao?
                    int nStarIndex = 0;
                    int nLengthSub = 0;
                    if (nNumberCut ==-1)
                    {
                        for (int i = 0; i < nNumberReplace; i++)
                        {

                            nLengthSub = arrPosition[(i * nNumberWord) + (nNumberWord - 1)];
                            sSplit.Add(sInput.Substring(nStarIndex, nLengthSub - nStarIndex).Trim());
                            nStarIndex = arrPosition[(i * nNumberWord) + (nNumberWord - 1)];
                        }    
                    }
                    else
                    {
                        for (int i = 0; i < nNumberCut; i++)
                        {

                            nLengthSub = arrPosition[(i * nNumberWord) + (nNumberWord - 1)];
                            sSplit.Add(sInput.Substring(nStarIndex, nLengthSub - nStarIndex).Trim());
                            nStarIndex = arrPosition[(i * nNumberWord) + (nNumberWord - 1)];
                        }  
                    }

                    sSplit.Add(sInput.Substring(nStarIndex, sInput.Length - nLengthSub).Trim());
                    StringBuilder sEdit = new StringBuilder();
                    for (int j = 0; j < sSplit.Count-1; j++)
                    {
                        sEdit.Append(sSplit[j]);
                        sEdit.Append(sReplace);
                    }
                    sEdit.Append(sSplit[sSplit.Count - 1]);
                    return sEdit.ToString();
                }
            }
            return sInput;
        }

        private static int[] GetPositionParttern(string sInput, string sPattern)
        {
            int[] arrPosition = new int[]{};
            Match m = Regex.Match(sInput, sPattern);
            while (m.Success)
            {
                Array.Resize<int>(ref arrPosition, arrPosition.Length + 1);
                arrPosition[arrPosition.Length - 1] = m.Index;
                m = m.NextMatch();
            }
            return arrPosition;
        }

        public static void DeleteFile(string sPath, string sFileName)
        {
            try
            {
                DirectoryInfo dirInfo = new DirectoryInfo(sPath);
                FileInfo[] arrfInfo = dirInfo.GetFiles();
                foreach (var item in arrfInfo)
                {
                    if (!item.Name.ToLower().Contains(sFileName.ToLower()))
                    {
                        item.Delete();
                    }
                }
            }
            catch (Exception)
            {

            }

        }

        public static void DeleteFolder(string sPath, string sFolderName)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(sPath + "/" + sFolderName);
            dirInfo.Create();
            DirectoryInfo[] directoryInfo = new DirectoryInfo(sPath).GetDirectories();
            try
            {
                foreach (var s in directoryInfo)
                {
                    if (s.Name.ToLower().Trim() != sFolderName.ToLower().Trim())
                    {
                        Directory.Delete(s.FullName,true);
                    }
                }
            }
            catch (Exception)
            {
                
                throw;
            }
            
            
        }
        public static int PageNumber(int total, int rows)
        {
            int nPage = total / rows;
            if (total % rows ==0)
            {
                nPage = total / rows;
            }
            else
            {
                nPage++;
            }
            return nPage;
        }
    }
}